package cn.smartorigin.soas.frame;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class JColorTable extends JTable{


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int rowHeight = 30;		//行高度
	private int idColWidth = 20;		//行高度
	
	private JPopupMenu popup = null;  
		
	public JColorTable(TableModel tableModel){
		super(tableModel);
		this.setGridColor(gridColor);
		this.setRowHeight(rowHeight);
		this.setColumnSelectionAllowed(false);
		this.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS); 
		
		// table event
		 this.addMouseListener(new MouseAdapter() {
			 public void mouseClicked(MouseEvent e) {
				 if(e.getButton() == MouseEvent.BUTTON3) {  	 
					 // right button selected row
		             int   row   =   JColorTable.this.rowAtPoint(e.getPoint());
		             JColorTable.this.setRowSelectionInterval(row, row);
		             
		             // popup menu
					 getPopup().show(e.getComponent(), e.getX(), e.getY());  
				 }
			 }
		 });
	}
	
	/**
	 * 表格不可编辑
	 */
	@Override
	public boolean isCellEditable(int row, int column) {				// 表格不可编辑
		return false;
	}
	
	@Override
	public TableCellRenderer getCellRenderer(int row, int column) {
//		return super.getCellRenderer(row, column);
		return new MyCellRenderer();
	}

	class MyCellRenderer extends DefaultTableCellRenderer {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			
			Component cell = super.getTableCellRendererComponent(table, value,
					isSelected, hasFocus, row, column);
			
			this.setColor(cell, table, isSelected, hasFocus, row, column);
			this.setWidth(cell, table, isSelected, hasFocus, row, column);
			//this.setButton(cell, table, isSelected, hasFocus, row, column);
			this.setHorizontalAlignment(SwingConstants.CENTER);
			return cell;
		}

		/*
		 * 设置颜色
		 */
		private void setColor(Component component, JTable table,
				boolean isSelected, boolean hasFocus, int row, int column) {
			if (isSelected) {
				component.setBackground(SoasUtil.selectionColor);
				setBorder(null);							// 去掉边
			} else {
				if (row % 2 == 0) {
					component.setBackground(SoasUtil.evenRowColor);
				} else {
					component.setBackground(SoasUtil.oddRowColor);
				}
			}
		}
		
		/**
		 * 设置宽度
		 */
		private void setWidth(Component component, JTable table,
				boolean isSelected, boolean hasFocus, int row, int column) {
			if (column == 0){
				TableColumnModel   cm   =   table.getColumnModel();     //表格的列模型 
				TableColumn   col   =   cm.getColumn(column);            //第i个列对象 
				col.setPreferredWidth(idColWidth);

			}else if(column == 6 ||column == 8 ){
				component.setForeground(SoasUtil.colunmHighRigthColor);
			}
		}
		
		/**
		 * 添加操作按钮
		 */
		private void setButton(Component component, JTable table,
				boolean isSelected, boolean hasFocus, int row, int column) {

			if(column == (table.getColumnCount()-1) ){
				TableColumn col = new TableColumn();
				table.addColumn(col);
			}

		}
		
		
	}
	
	/**
	 * 弹出菜单
	 * @return 弹出菜单对象 
	 */
	private JPopupMenu getPopup() {
		if (popup == null) {
			popup = new JPopupMenu("Popup");

			JMenuItem item1 = new JMenuItem("修改");
			item1.setBackground(Color.white);
			item1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int rowId =	JColorTable.this.getSelectedRow();
					ColorTableIF container = (ColorTableIF) JColorTable.this.getParent().getParent().getParent();
					container.TableModify(rowId);
				}
			});
			popup.add(item1);

			JMenuItem item2 = new JMenuItem("删除");
			item2.setBackground(Color.white);
			item2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					int result = JOptionPane.showConfirmDialog(null, "确认删除", "警告", JOptionPane.YES_NO_OPTION);
					if(result==JOptionPane.YES_OPTION ){
						int rowId =	JColorTable.this.getSelectedRow();
						ColorTableIF container = (ColorTableIF) JColorTable.this.getParent().getParent().getParent();
						container.TableDelete(rowId);
					}
					

				}
			});
			popup.add(item2);

		}
		return popup;
	}
	
}
